//
//  YTSettingController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/27.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTSettingController.h"
#import "YTFileTools.h"

@interface YTSettingController ()
@property (weak, nonatomic) IBOutlet UIView *sliderContentView;
@property (weak, nonatomic) IBOutlet UIView *logoutContentView;

@property (weak, nonatomic) IBOutlet UILabel *cacheSizeLabel;
@property(nonatomic ,strong)StepSlider *stepSlider;
@end

@implementation YTSettingController

// lazy stepSlider
- (StepSlider *)stepSlider
{
    if (!_stepSlider) {
        
        [self.view layoutIfNeeded];

        _stepSlider = [[StepSlider alloc] initWithFrame:CGRectMake(0, 8, self.sliderContentView.bounds.size.width,
                                                                   self.sliderContentView.bounds.size.height - 8)];
        
        _stepSlider.trackColor = [UIColor colorWithRed:167.0f/255.0f
                                                 green:178.0f/255.0f
                                                  blue:179.0f/255.0f
                                                 alpha:1];
        
        _stepSlider.sliderCircleColor = [UIColor colorWithRed:212.0f/255.0f
                                                        green:226.0f/255.0f
                                                         blue:227.0f/255.0f
                                                        alpha:1];
        
        _stepSlider.tintColor = [UIColor colorWithRed:30.0f/255.0f
                                                green:171.0f/255.0f
                                                 blue:179.0f/255.0f
                                                alpha:1];
   
        _stepSlider.trackHeight = 10;
    
        [_stepSlider setMaxCount:3];
    }
    return _stepSlider;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // 判断是否退出登录
    self.logoutContentView.hidden = YTNetworkManager.shareManager.account.auth ? NO : YES;
    
    switch (YTNetworkManager.shareManager.textSize)
    {
        case 60: // 小
            [self.stepSlider setIndex:0];
            break;
        case 100: // 中
            [self.stepSlider setIndex:1];
            break;
        default: // 大
            [self.stepSlider setIndex:2];
            break;
    }
    
    [self.sliderContentView addSubview:self.stepSlider];
    
    [self.stepSlider addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
    
    self.navigationItem.title = @"设置";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage dh_setNavImageName:@"倛袨1"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick)];

    
    // 计算缓存
    [YTFileTools getFileSize:cachePath
                  completion:^(NSString *totalSize)
    {
        self.cacheSizeLabel.text = totalSize;
    }];
}

// 返回按钮
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:true];
}

#pragma mark: - 退出登录按钮
- (IBAction)logoutButtonClick
{
    
    [YTNetworkManager.shareManager requestLogOutWithCompletion:^(BOOL isSucc, NSString *StatusInfo) {
        if (!isSucc) {
            [self.view showTipWithTipStr:StatusInfo];
            return ;
        }
        [[NSFileManager defaultManager] removeItemAtPath:[YTUserAccountJson dh_appendDocumentDir] error:nil];
        YTNetworkManager.shareManager.account = nil;
        [[YTNetworkManager shareManager].requestSerializer setValue:nil forHTTPHeaderField:@"user-token"];
        [YTNetworkManager.shareManager getAccessTokeWithCompletion:^(BOOL isSucc, NSString *error) {
            
        }];
        [self.view showTipWithTipStr:StatusInfo];
        [self.navigationController popViewControllerAnimated:true];
        
    }];
 
}


#pragma mark: - slider监听
- (void)sliderChange:(StepSlider *)sender
{
    switch (sender.index)
    {
        case 0: // 小
            YTNetworkManager.shareManager.textSize = 60;
            break;
        case 1: // 中
            YTNetworkManager.shareManager.textSize = 100;
            break;
        default: // 小
            YTNetworkManager.shareManager.textSize = 150;
            break;
    }
}

#pragma mark: - table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0: // 评分
       
        {
            NSString* nsStringToOpen = [NSString  stringWithFormat: @"itms-apps://itunes.apple.com/app/id%@?action=write-review",@"1423040714"];//替换为对应的APPID
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:nsStringToOpen]];
            break;
        }
        case 1:
            [self showAlertView];
            break;
        default:
            break;
    }
}

- (void)showAlertView
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清除缓存"
                                                                   message:[NSString stringWithFormat:@"已缓存%@，确定清理吗？",self.cacheSizeLabel.text]
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定"
                                                            style:UIAlertActionStyleDestructive
                                                          handler:^(UIAlertAction * _Nonnull action)
    {
        [YTFileTools removeDirectoryPath:cachePath];
        [YTFileTools getFileSize:cachePath completion:^(NSString *totalSize) {
            self.cacheSizeLabel.text = totalSize;
        }];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                           style:UIAlertActionStyleCancel
                                                         handler:nil];
    
    [alert addAction:confirmAction];
    [alert addAction:cancelAction];
    
    
    [self presentViewController:alert animated:true completion:nil];
    //[alert pres]
}



@end
